﻿#ifndef ANIMATIONCONTROLLER_H
#define ANIMATIONCONTROLLER_H

#include <QObject>
#include <QColor>
#include <cmath>
#include <QTimer>
#include <QElapsedTimer>
#include "pipeGraph.h"
#include "animationState.h"
#include "syringeCalculator.h"

class AnimationController : public QObject
{

    Q_OBJECT
public:
    AnimationController(AnimationState* state);
    ~AnimationController();

    // 工作模式1 手动调帧
    void elapseFrame(int i = 0);         //  1. 手动调帧
    Q_INVOKABLE QVariantMap requestInfo(int frameIdx);            // [接口] 获取当前状态信息
    int getFrameNum();
    void clearFrameState();


    // 工作模式2 启用Timer
    void startTimer();                   //  2. 启动Timer
    Q_INVOKABLE QVariantMap requestInfo();                        // [接口] 获取当前状态信息


    void crashStop();                   // 急停（貌似已弃用

public slots:
    void on_reset();
    void on_wheelChanged(int posi1, int posi2, double speed, int direct);
    void on_hatPressed();
    void on_hatPushed();
    void on_valveChanged(int idx, bool state1, bool state2);
    void on_syringeChanged(int idx, double percentage1, double percentage2, double speed);
    void on_liquidChanged(QMap<QString, QVector<int>> pathList);

    void updateState();         // 状态计算更新

private:

    QTimer*         _timer = nullptr;
    QElapsedTimer*  _timer_e = nullptr;     // 时间核算

    AnimationState* _state = nullptr;

    QVector<double>  _valveAngle;
    QVector<double>  _valveEnd;


    // 注射器
    QVector<double> _syringePercent;
    QVector<double> _syringePercentEnd;
    QVector<double> _syringePercentSpeed;
    QVector<bool>   _syringeIsPushing;

    // 转盘
    double          _wheel;
    double          _wheelEnd;
    double          _wheelSpeed;
    double          _wheelDirect;


    // 瓶帽
    double          _hatPressPosi;
    int             _hatPressState;
    double          _hatPushPosi;
    int             _hatPushState;
    QVector<bool>   _bottleHasHat;

    bool            _refreshPosi = false;   // 在发送一个新的状态时，是否立即更新到旧的状态

    // 色谱柱粒子特效
    int             _particleNum = 6;
    QVector<double> _particle;
    QVector<double> _particle_speed = {1, 1.4, 1.8, 1.6, 1.2, 0.9};
    // 检测器特承诺
    double          _detectorAngle = 0;

    double          _interval = 40;     // 理论间隔
    double          _interval_real;     // 实际间隔
    double          _lastTime = 0;

    // 管道液体流动状态
    QMap<QString, stLiquidPath>  _pathList;


    QMap<int, QVariantMap>      _frameState;


};

#endif          // ANIMATIONCONTROLLER_H
